package com.alander.nexusgo.im.netty.codec;

import com.alander.nexusgo.im.model.dto.ImMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ImMessageEncoder extends MessageToByteEncoder<ImMsg> {
    
    @Override
    protected void encode(ChannelHandlerContext ctx, ImMsg msg, ByteBuf out) throws Exception {
        // 写入魔数
        out.writeShort(msg.getMagic());
        // 写入消息代码
        out.writeInt(msg.getCode());
        // 写入body长度
        out.writeInt(msg.getLen());
        // 写入body数据
        out.writeBytes(msg.getBody());
    }
}